import {
  getBaseUrl,
  requestUtil,
  requestLocation
} from '../../utils/requestUtil.js'


Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  mapConfig: {
    // 使用在腾讯位置服务申请的key
    key: 'DG2BZ-NTRKZ-MEDXK-7NNQB-5W24O-ITBL7',
    // 调用插件的app的名称
    referer: '何家俊的生鲜购物系统',
    // 是否启用智能规划
    enableAI: true,
    // 是否开启导航功能
    navigation: 1,
    // 默认终点
    endPoint: {
      name: '长沙南站',
      latitude: 28.147104,
      longitude: 113.065494,
    },
  },


  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const {
      orderId
    } = options
    this.setData({
      baseUrl: getBaseUrl()
    })
    await this.getOrderDetail(orderId)
    await this.getLocation()
  },

  async getOrderDetail(orderId) {
    const res = await requestUtil({
      url: '/rider/order/getOrderDetail',
      data: {
        orderId
      }
    })
    let totalNum = 0;
    res.order.goods.forEach(v => {
      totalNum += v.goodsNumber
    })
    const {
      name
    } = wx.getStorageSync('userInfo')
    this.setData({
      order: res.order,
      totalNum,
      riderName: name
    })
  },

  // 地址解析
  async getLocation() {
    if (!this.data.order?.address) {
      console.error('订单地址未找到');
      return;
    }
    if (this.data.order.status != 4) {
      console.log('无需进行路线规划');
      return;
    }
    const res = await requestLocation({
      data: {
        key: this.mapConfig.key,
        address: this.data.order.address
      }
    })
    console.log(res)
    this.mapConfig.endPoint = {
      name: res.title,
      latitude: res.location.lat,
      longitude: res.location.lng
    };
  },

  openMap() {
    const {
      key,
      referer,
      enableAI,
      navigation,
      endPoint
    } = this.mapConfig
    const endPointStr = JSON.stringify(endPoint)
    wx.navigateTo({
      url: `plugin://route-plan/index?key=${key}&referer=${referer}&endPoint=${endPointStr}&enableAI=${enableAI}&navigation=${navigation}`,
    });
  },

  //图片预览
  handleImagePreview(e) {
    const baseUrl = `${this.data.baseUrl}/img/product/`;
    const urls = this.data.order.goods.map(v => baseUrl + v.goodsPic);
    const {
      currenturl
    } = e.currentTarget.dataset;
    wx.previewImage({
      current: currenturl,
      urls,
    })
  },

  async handleAccept(e) {
    const {
      id
    } = e.currentTarget.dataset
    console.log(id)
    wx.showModal({
      title: '系统提示',
      content: '您确定要进行接单操作吗？',
      complete: async (res) => {
        if (res.confirm) {
          const res = await requestUtil({
            url: '/rider/order/updateOrderStatus',
            data: {
              orderId: id,
              status: 4
            }
          })
          await wx.showToast({
            title: '接单成功'
          })
          wx.switchTab({
            url: `/pages/index/index?status=${3}`,
          })
        }
      }
    })
  },

  async handleDelivery(e) {
    const {
      id
    } = e.currentTarget.dataset
    console.log(id)
    wx.showModal({
      title: '系统提示',
      content: '您确定完成配送操作吗？',
      complete: async (res) => {
        if (res.confirm) {
          const res = await requestUtil({
            url: '/rider/order/updateOrderStatus',
            data: {
              orderId: id,
              status: 5
            }
          })
          await wx.showToast({
            title: '配送完成'
          })
          wx.switchTab({
            url: `/pages/index/index?status=${4}`,
          })
        }
      }
    })
  },

  handleCall(e) {
    const {
      phone
    } = e.currentTarget.dataset
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

})